package com.fengxu.gamescript.ui.edit

import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.fengxu.gamescript.R
import com.xuexiang.xui.utils.DensityUtils
import org.jetbrains.anko.padding
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.textColor
import kotlin.properties.Delegates

/**
 * 只使用TextView搭建RecyclerView列表的适配器
 */
class TextViewListAdapter: RecyclerView.Adapter<TextViewListAdapter.TextViewHolder> {

    /**
     * 只使用TextView
     */
    class TextViewHolder(view:TextView):RecyclerView.ViewHolder(view)

    private var mContext: Context

    var datas: Array<String>

    private var width by Delegates.notNull<Int>()

    private var height by Delegates.notNull<Int>()

    // 点击子项事件
    var clickItem:((position: Int,view: TextView)->Unit)? = null

    constructor(ctx:Context, datas: Array<String>,  width:Int, height:Int){
        this.mContext = ctx
        this.datas = datas
        this.width = width
        this.height = height
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TextViewHolder {
        val tv = TextView(mContext)
        tv.textSize = 18F
        tv.textColor = Color.BLACK
        val pd = DensityUtils.dp2px(5F)
        tv.setPadding(pd,0,pd,0)
        tv.gravity = Gravity.CENTER
        tv.layoutParams = ViewGroup.LayoutParams(width, height)
        tv.background = mContext.resources.getDrawable(R.drawable.btn_bg,null)
        return TextViewHolder(tv)
    }

    override fun onBindViewHolder(holder: TextViewHolder, position: Int) {
        val tv = holder.itemView as TextView
        tv.text = datas[position]
        tv.onClick {
            clickItem?.invoke(position, holder.itemView)
        }
    }

    override fun getItemCount(): Int {
        return datas.size
    }

}